package com.amokgames.tictactoe;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Util {
	// source...
	//   http://stackoverflow.com/questions/5980658/how-to-sha1-hash-a-string-in-android
	
    public static String bytesToHex(byte[] data) { 
        StringBuffer buffer = new StringBuffer();
        for (int i= 0; i < data.length; i++) { 
            int halfByte= (data[i] >>> 4) & 0x0F;
            int twoHalfs= 0;
            do { 
                if ((0 <= halfByte) && (halfByte <= 9)) 
                    buffer.append((char) ('0' + halfByte));
                else 
                    buffer.append((char) ('a' + (halfByte - 10)));
                halfByte = data[i] & 0x0F;
            } while(twoHalfs++ < 1);
        } 
        return buffer.toString();
    }

    public static byte[] SHA1(String text) 
    		throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    	MessageDigest md;
    	md= MessageDigest.getInstance("SHA-1");
    	byte[] sha1Hash= new byte[40];
    	md.update(text.getBytes("iso-8859-1"), 0, text.length());
    	sha1Hash= md.digest();
    	return sha1Hash;
    } 
}
